﻿using System;
using uForum.Businesslogic;
using uForum.Businesslogic.Services;

namespace uForum.usercontrols.uForum
{
	public partial class NewForumTopic : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			AuthorizationService auth = new AuthorizationService();
			Rights rights = auth.GetRights(ForumId);
			if(!rights.CanPost)
			{
				mvwControls.SetActiveView(vwLogin);
			}
		}

		protected void create_Click(object sender, EventArgs e)
		{
			PostingService service = new PostingService();

			int forumid = ForumId;
			string title = titleTextbox.Text;
			string body  = topicBodyTextbox.Text;
			string topicUrl = service.NewTopic(forumid, title, body);
			if(topicUrl.Equals("0"))
			{
				mvwControls.SetActiveView(vwError);
			}
			else
			{
				Response.Redirect(topicUrl);
			}
		}

		protected int ForumId
		{
			get { return umbraco.presentation.nodeFactory.Node.GetCurrent().Id; }
		}
	}
}